package com.AUI.components
{
	import com.AUI.*;
	
	import flash.events.*;
	/**
	 * Scroll component.
	 */
	public class ScrollBase extends AComponent
	{
		/**
		 * [element] scrollbars: horizontal scrollbar(0 index) and vertical scrollbar(1 index).
		 */
		public var e_scrollbars:Vector.<ScrollBar> = new Vector.<ScrollBar>(2);
		/**
		 * [element] corner button,showed in right-bottom position when horizontal scrollbar and vertical scrollbar are visible.
		 */
		public var e_corner:Button;
		/**
		 * Constructor.
		 */
		public function ScrollBase()
		{
			// init attributes
			depth = 1;
			align = true;
			
			/**************** INITCOMPLETE ****************/
			Object(this).constructor == ScrollBase && initComplete();
		}
		/**
		 * Set the horizontal scrollbar of container.
		 * @param scrollbar Indicates that the scrollbar will be used,null is no need.
		 * @see com.AUI.components.AScrollBar
		 */
		public function set horizontalScrollBar(scrollbar:ScrollBar):void
		{
			this.setScrollBar(0,scrollbar);
		}
		public function get horizontalScrollBar():ScrollBar
		{
			return e_scrollbars[0];	
		}
		/**
		 * Set the vertical scrollbar of container.
		 * @param scrollbar Indicates that the scrollbar will be used,null is no need.
		 * @see com.AUI.components.AScrollBar
		 */
		public function set verticalScrollBar(scrollbar:ScrollBar):void
		{
			this.setScrollBar(1,scrollbar);
		}
		public function get verticalScrollBar():ScrollBar
		{
			return e_scrollbars[1];	
		}
		/**
		 * Install a horizontal scrollbar into container.
		 * @param type Indicates that the scrollbar class.
		 * @see com.AUI.components.AScrollBar
		 * @see com.AUI.define.AScrollBarType
		 */
		private function setScrollBar(index:uint,scrollbar:ScrollBar):void
		{
			if(scrollbar == null)
			{
				scrollbar = e_scrollbars[index];
				// initialize
				scrollbar.detach();
				e_scrollbars[index] = null;
				e_scrollbars[0] || e_scrollbars[1] || removeEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheel);
				scrollbar.parent && removeChild(scrollbar);
			}
			else
			{
				scrollbar.disabled = this.disabled;
				// initialize
				e_scrollbars[0] || e_scrollbars[1] || addEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheel);
				e_scrollbars[index] = scrollbar;
				scrollbar.attach(boxModel.content,elements);
			}
			updateBoxModel();
		}
		/**
		 * Set the corner button of container,the corner button is be slowed when horizontal scrollbar and vertical scrollbar are available.
		 * @param scrollbar Indicates that the scrollbar will be used,null is no need.
		 * @see com.AUI.components.AScrollBar
		 */
		public function set cornerButton(button:Button):void
		{
			if(button == null)
			{
				button = e_corner;
				// initialize
				button.removeEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheelCorner);
				button.removeEventListener(MouseEvent.CLICK,onClickCorner);
				e_corner = null;
				button.parent && removeChild(button);
			}
			else
			{
				e_corner = button;
				// initialize
				button.addEventListener(MouseEvent.CLICK,onClickCorner,false,0,true);
				button.addEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheelCorner,false,0,true);
			}
			updateBoxModel();
		}
		public function get cornerButton():Button
		{
			return e_corner;
		}
		/**
		 * @private
		 */
		override protected function resizeBoxModel():void
		{
			super.resizeBoxModel();
			var HB:ScrollBar = e_scrollbars[0]; // horizontal bar
			var VB:ScrollBar = e_scrollbars[1]; // vertical bar
			var CB:Button    = e_corner;		 // corner button
			var OW:Boolean	  = HB && boxModel.content.height > HB.height && isOverflowWidth();
			var OH:Boolean	  = VB && boxModel.content.width  > VB.width  && isOverflowHeight();
			// 1.set scrollbars
			if(OW){ boxModel.content.height -= HB.height; }
			if(OH){ boxModel.content.width  -= VB.width;  }
			if(OW)
			{
				HB.readjustThumb();
				HB.parent || addChild(HB);
			}
			else
			{
				elements.x = 0;
				HB && HB.parent && removeChild(HB);
			}
			if(OH)
			{	
				VB.readjustThumb();
				VB.parent || addChild(VB);
			}
			else
			{
				elements.y = 0;
				VB && VB.parent && removeChild(VB);
			}
			// 2.set corner button
			if(OW && OH && CB)
			{
				CB.parent || addChild(CB);
			}
			else
			{
				CB && CB.parent && removeChild(CB);
			}
			// 3.set alignable
			if(OW || OH)
			{
				align = false;
			}
			else
			{
				align = true;
			}
		}
		/**
		 * @private
		 */
		override protected function redrawBoxModel():void
		{
			super.redrawBoxModel();
			redrawScrollBar();
		}
		/**
		 * Call it to redraw scrollbar.
		 * @see #updateStyle()
		 */
		protected function redrawScrollBar():void
		{
			var HB:ScrollBar = e_scrollbars[0]; // horizontal bar
			var VB:ScrollBar = e_scrollbars[1]; // vertical bar
			var CB:Button    = e_corner;		 // corner button
			var OW:Boolean	  = HB ? Boolean(HB.parent) : false;
			var OH:Boolean	  = VB ? Boolean(VB.parent) : false;
			var OS:Boolean	  = CB ? (OW && OH) : false;
			// 1.set horizontal scrollbar
			if(OW)
			{
				HB.x	 = boxModel.content.x;
				HB.y	 = boxModel.content.y + boxModel.content.height;
				HB.width = boxModel.content.width;
			}
			// 2.set vertical scrollbar
			if(OH)
			{
				VB.y	  = boxModel.content.y;
				VB.x	  = boxModel.content.x + boxModel.content.width;
				VB.height = boxModel.content.height;
			}
			// 3.set corner button
			if(OS)
			{
				CB.x		= VB.x;
				CB.y		= HB.y;
				CB.width	= VB.width;
				CB.height	= HB.height;
			}
		}
		/**
		 * Called when MouseEvent.MOUSE_WHEEL event has been dispatched on component.
		 */
		protected function onMouseWheel(e:MouseEvent):void
		{
			var VB:ScrollBar = e_scrollbars[1];
			var HB:ScrollBar = e_scrollbars[0];
			if(VB && VB.parent)
			{
				VB.scrollWheel(e.delta);
				e.stopPropagation();
				return;
			}
			if(HB && HB.parent)
			{
				HB.scrollWheel(e.delta);
				e.stopPropagation();
				return;
			}
		}
		/**
		 * Called when MouseEvent.MOUSE_WHEEL event has been dispatched on corner button of component.
		 * @see #pCorner
		 */
		protected function onMouseWheelCorner(e:MouseEvent):void
		{
			var HB:ScrollBar = e_scrollbars[0];
			var VB:ScrollBar = e_scrollbars[1];
			HB.scrollWheel(e.delta);
			VB.scrollWheel(e.delta);
			e.stopPropagation();
		}
		/**
		 * Called when MouseEvent.CLICK event has been dispatched on corner button of component.
		 * @see #pCorner
		 */
		protected function onClickCorner(e:MouseEvent):void
		{
			var HB:ScrollBar = e_scrollbars[0]; // horizontal scrollbar
			var VB:ScrollBar = e_scrollbars[1]; // vertical scrollbar
			var EW:int = elements.width;
			var EH:int = elements.height;
			if(HB.value == 1 && VB.value == 1)
			{
				HB.scroll(-EW);
				VB.scroll(-EW);
			}
			else
			{
				HB.scroll(EW);
				VB.scroll(EW);
			}
		}
	}
}